package com.logic.modular.game.player.match;

import com.logic.comment.util.RedisUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class PlayerPvpBattleLog {
    private Long playerId;
    public Set<Long> battleIdSet;

    public static void logBattle(Long playerId, Long... battleId) {
        PlayerPvpBattleLog playerPvpBattleLog = obtainBattleLog(playerId);
        if (playerPvpBattleLog == null) {
            playerPvpBattleLog = new PlayerPvpBattleLog(playerId, new HashSet<>(Arrays.asList(battleId)));
        } else {
            playerPvpBattleLog.battleIdSet.addAll(Arrays.asList(battleId));
        }
        playerPvpBattleLog.save();
    }

    public static PlayerPvpBattleLog obtainBattleLog(Long playerId) {
        return (PlayerPvpBattleLog) RedisUtils.redisUtils().get(String.format("player:%d:battle", playerId));
    }

    /**
     * 重置pvp战斗记录id索引
     */
    public static void resetPvp(Long playerId) {
        RedisUtils.redisUtils().delete(String.format("player:%d:battle", playerId));
    }

    public void save() {
        RedisUtils.redisUtils().set(String.format("player:%d:battle", playerId), this, 1L, TimeUnit.DAYS);
    }
}
